package com.SouthernWall_404.age_tool.common.world.item;

import com.SouthernWall_404.age_tool.AgeTool;
import com.SouthernWall_404.age_tool.common.world.Entity.ExcuteBlockEntity;
import com.SouthernWall_404.age_tool.common.world.Entity.TriggerBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CommandBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;

public class ExcuteBlockItem extends BlockItem {

    public static final String TRIGGERPACK_TAG="trigger";
    public static final String ID_TAG="uuid";

    public ExcuteBlockItem(Block p_40565_, Properties p_40566_) {
        super(p_40565_, p_40566_);
    }



    @Override
    public @Nullable BlockPlaceContext updatePlacementContext(BlockPlaceContext blockPlaceContext) {

        return super.updatePlacementContext(blockPlaceContext);
    }

//    @Override
//    protected boolean updateCustomBlockEntityTag(BlockPos blockPos, Level level, @Nullable Player player, ItemStack itemStack, BlockState blockState) {
//
//        BlockEntity blockEntity=level.getBlockEntity(blockPos);
//        if(blockEntity instanceof ExcuteBlockEntity excuteBlockEntity)
//        {
//            excuteBlockEntity.remoteId=getRemoteId(itemStack);
//
//        }
//
//
//
//
//        return super.updateCustomBlockEntityTag(blockPos, level, player, itemStack, blockState);
//    }

    @Override
    protected boolean updateCustomBlockEntityTag(BlockPos pos, Level level, @Nullable Player player, ItemStack stack, BlockState state) {
        // 首先调用父类方法
        boolean result = super.updateCustomBlockEntityTag(pos, level, player, stack, state);

        if (!level.isClientSide) {
            // 获取方块实体
            BlockEntity blockEntity = level.getBlockEntity(pos);

            if (blockEntity instanceof ExcuteBlockEntity excuteBlockEntity) {
                    excuteBlockEntity.remoteId=getRemoteId(stack);
                    excuteBlockEntity.setChanged();
                    level.sendBlockUpdated(pos, state, state, 3);
                }
            }
        return result;
    }

    // 可选：重写此方法以在创造模式中放置方块时也应用NBT
    @Override
    public boolean placeBlock(net.minecraft.world.item.context.BlockPlaceContext context, BlockState state) {
        boolean result = super.placeBlock(context, state);

        if (result && !context.getLevel().isClientSide()) {
            // 获取位置和物品栈
            BlockPos pos = context.getClickedPos();
            ItemStack stack = context.getItemInHand();

            // 获取方块实体
            BlockEntity blockEntity = context.getLevel().getBlockEntity(pos);

            if (blockEntity instanceof ExcuteBlockEntity excuteBlockEntity) {
                    excuteBlockEntity.remoteId=getRemoteId(stack);
                    excuteBlockEntity.setChanged();
                    context.getLevel().sendBlockUpdated(pos, state, state, 3);
                }
            }


        return result;
    }

    public static UUID getRemoteId(ItemStack itemStack)
    {
        UUID remoteId=null;
        CompoundTag nbt=itemStack.getOrCreateTag();
        if(nbt.contains(AgeTool.MODID))
        {
            CompoundTag modPack=nbt.getCompound(AgeTool.MODID);
            if(modPack.contains(TRIGGERPACK_TAG))
            {
                CompoundTag triggerPack=modPack.getCompound(TRIGGERPACK_TAG);
                if(triggerPack.contains(ID_TAG))
                {
                    remoteId=triggerPack.getUUID(ID_TAG);
                }
                else
                {
                    remoteId=null;
                }
            }

        }
        return remoteId;
    }

    public static void setRemoteId(ItemStack itemStack,UUID remoteId)
    {
        CompoundTag triggerPack=new CompoundTag();
        triggerPack.putUUID(ID_TAG,remoteId);
        CompoundTag modPack=new CompoundTag();
        modPack.put(TRIGGERPACK_TAG,triggerPack);
        CompoundTag nbt=itemStack.getOrCreateTag();
        nbt.put(AgeTool.MODID,modPack);

    }

    @Override
    public void appendHoverText(ItemStack itemStack, @Nullable Level p_40573_, List<Component> tooltips, TooltipFlag p_40575_) {

        UUID uuid=getRemoteId(itemStack);
        if(uuid!=null)
        {
            tooltips.add(Component.translatable("id:"+uuid)) ;
        }

        super.appendHoverText(itemStack, p_40573_, tooltips, p_40575_);
    }
}
